#include "main.h"
#include "CWorld.h"
#include "Functions.h"
#include "Global.h"

namespace GUI
{
	void GUIEvent( UINT GUIType, UINT Event, CGUIBase* Src, void* Dest){
		switch(GUIType){
		case GUITYPE_WINDOW:
			break;
		case GUITYPE_TEXTBOX:
			switch(Event){
			case EVENT_TOGGLEFULLSCREEN:
			case EVENT_NEWGAME:
			case EVENT_LOADGAME:
			case EVENT_SAVEGAME:
			case EVENT_EXITGAME:
				break;
			case EVENT_CONSOLE:
				((CConsole*)Dest)->Command((CGUITextBox*)Src);
				break;
			case EVENT_SETGLOBAL:
					break;
			}
		case GUITYPE_RADIO:
			switch(Event){
			case EVENT_SETGLOBAL:
					break;
			}
		case GUITYPE_CHECKBOX:
			switch(Event){
			case EVENT_TOGGLEFULLSCREEN:
				//fullscreen=((CGUICheckBox*)Src)->GetChecked();
				break;
			case EVENT_SETGLOBAL:
				break;
			}
		case GUITYPE_BUTTON:
			switch(Event){
			case EVENT_TOGGLEFULLSCREEN:
			case EVENT_NEWGAME:
				Src->GetParent()->OnFocusOut();
				Src->GetParent()->SetEnabled(false);
				Src->GetParent()->SetVisible(false);
				((CLevel*)Dest)->DumpLevel();
				((CLevel*)Dest)->LoadLevel("Resources/Levels/level1.mel");
				break;
			case EVENT_LOADGAME:
				((CLevel*)Dest)->DumpLevel();
				((CLevel*)Dest)->LoadLevel("Resources/Levels/level1.mel");
				break;
			case EVENT_SAVEGAME:
				break;
			case EVENT_EXITGAME:
				((CWorld*)Dest)->ExitGame();
				break;
			case EVENT_OPENWINDOW:
				((CGUIWindow*)Dest)->SetEnabled(true);
				((CGUIWindow*)Dest)->RequestFocus();
				break;
			case EVENT_CLOSEWINDOW:
				((CGUIWindow*)Dest)->OnFocusOut();
				((CGUIWindow*)Dest)->SetEnabled(false);
				((CGUIWindow*)Dest)->SetVisible(false);
				break;
			}
		}
	}
}

CWorld* g_pWorld = NULL;

CWorld::CWorld(){
	ShowCursor(false);
	SetupSprites(g_pSpriteList);
	SetupFonts(g_pFontList);
	m_pCurrentLevel = new CLevel();
	m_pConsole=new CConsole( GUIEvent );

	GUI_COLORS Colors;
	ZeroMemory(&Colors, sizeof(GUI_COLORS));
	Colors.BG=D3DCOLOR_ARGB(128,0,0,0);

	GUI_PARAMS Params;
	ZeroMemory(&Params, sizeof(GUI_PARAMS));
	Params.pos=V0;
	Params.width=WINDOW_WIDTH;
	Params.height=WINDOW_HEIGHT;
	Params.enabled=true;
	Params.visible=true;
	Params.parent=NULL;
	m_pGUIManager=new CGUIWindow(&Params, &Colors, NULL, false, "");
	m_pGUIManager->SetEventFunction( GUIEvent );
	m_pGUIManager->Add(m_pConsole->GetWindow());

	ZeroMemory(&Colors, sizeof(GUI_COLORS));
	Colors.Text=CLR_WHITE;

	ZeroMemory(&Params, sizeof(GUI_PARAMS));
	Params.pos=V(100, 400);
	Params.width=96;
	Params.height=24;
	Params.enabled=true;
	Params.visible=true;
	Params.parent=m_pGUIManager;

	m_pGUIManager->Add(new CGUIButton(&Params, &Colors, NULL, EVENT_NEWGAME, m_pCurrentLevel, "New Game"));
	Params.pos=V(100, 432);
	m_pGUIManager->Add(new CGUIButton(&Params, &Colors, NULL, EVENT_LOADGAME, m_pCurrentLevel, "Load Game"));
	Params.pos=V(100, 464);
	m_pGUIManager->Add(new CGUIButton(&Params, &Colors, NULL, EVENT_OPENWINDOW, m_pConsole->GetWindow(), "Options"));
	Params.pos=V(100, 496);
	m_pGUIManager->Add(new CGUIButton(&Params, &Colors, NULL, EVENT_EXITGAME, this, "Exit Game"));

	m_bKeyPressed=false;
	m_bExitApp=false;
	g_pCursor=g_pSpriteList->Misc->CursorDefault;
};
CWorld::~CWorld(){};
/*-------------------------------------------------------------------
-World Step
-------------------------------------------------------------------*/
void CWorld::Step(BYTE _keys[256], DIMOUSESTATE _mouse)
{
	if(_keys[DIK_ESCAPE]){
		if(!m_bKeyPressed){
			m_bKeyPressed=true;
			if(!m_pGUIManager->GetEnabled()){
				m_pGUIManager->SetVisible(true);
				m_pGUIManager->SetEnabled(true);
			}else if(m_pCurrentLevel->GetLoaded()){
				m_pGUIManager->SetVisible(false);
				m_pGUIManager->SetEnabled(false);
			}
		}
	}
	else if(_keys[DIK_GRAVE]){
		if(!m_bKeyPressed){
			m_bKeyPressed=true;
			if(!m_pGUIManager->GetEnabled()){
				m_pGUIManager->SetVisible(true);
				m_pGUIManager->SetEnabled(true);
				m_pConsole->GetWindow()->SetEnabled(true);
				m_pConsole->GetWindow()->RequestFocus();
			}else{
				if(!m_pConsole->GetWindow()->GetEnabled()){
					m_pConsole->GetWindow()->SetEnabled(true);
					m_pConsole->GetWindow()->RequestFocus();
				}else{
					m_pConsole->GetWindow()->OnFocusOut();
					m_pConsole->GetWindow()->SetVisible(false);
					m_pConsole->GetWindow()->SetEnabled(false);
				}
			}
		}
	}else m_bKeyPressed=false;


	GetCursorPos(&g_cursorPos);
	if(_mouse.rgbButtons[1]){
		m_pConsole->addLine("g_pCursor X: "+toStr((int)g_cursorPos.x)+" Y: "+toStr((int)g_cursorPos.y));
	}
	if(!m_pGUIManager->GetEnabled()){
		g_pCursor=g_pSpriteList->HUD->Crosshair;
		m_pCurrentLevel->Step(_keys, _mouse);
		
	}else{
		g_pCursor=g_pSpriteList->Misc->CursorDefault;
		m_pGUIManager->Step();
	}
};
/*-------------------------------------------------------------------
-World Draw
-------------------------------------------------------------------*/
void CWorld::Draw(){
	
	g_pDrawSprite->Begin(D3DXSPRITE_ALPHABLEND);
	g_pDrawLine->Begin();

	m_pCurrentLevel->Draw();
	m_pGUIManager->Draw();
	
	DrawSpriteExact(g_pCursor, 0, V((float)g_cursorPos.x, (float)g_cursorPos.y));

	g_pCollisionMan->DebugDraw();
	g_pDrawLine->End();
	g_pDrawSprite->End();
};

bool CWorld::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
	if(m_pGUIManager)
		return m_pGUIManager->WndProc(hWnd, uMsg, wParam, lParam);
	return false;
}

void CWorld::CleanUp(){
	m_pCurrentLevel->CleanUp();
	CleanupSprites(g_pSpriteList);
	delete g_pSpriteList;
	CleanupFonts(g_pFontList);
	delete g_pFontList;
};

